package com.plugin.manage.controller;

import com.plugin.manage.common.PageResult;
import com.plugin.manage.common.RespObj;
import com.plugin.manage.service.IConsultationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 咨询管理控制器
 *
 * @author mark
 * @date 2025-07-30
 */
@Tag(name = "咨询管理", description = "咨询相关接口")
@RestController
@RequestMapping("/consultation")
public class ConsultingController {

    @Autowired
    private IConsultationService consultationService;

    /**
     * 获取咨询记录总数
     *
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 咨询记录总数
     */
    @Operation(summary = "获取咨询记录总数", description = "统计elder_consultation表中指定时间范围内的记录总数")
    @GetMapping("/totalCount")
    public RespObj<?> getTotalConsultationCount(
            @Parameter(description = "开始时间", required = true, example = "2025-01-01 00:00:00")
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @Parameter(description = "结束时间", required = true, example = "2025-12-31 23:59:59")
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        Long totalCount = consultationService.getTotalConsultationCount(startTime, endTime);
        return new RespObj<Long>().success(totalCount);
    }

    /**
     * 获取正在跟进中的咨询记录数量
     *
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 正在跟进中的咨询记录数量
     */
    @Operation(summary = "获取正在跟进中的咨询记录数量", description = "统计elder_consultation表中指定时间范围内status=1的记录数量")
    @GetMapping("/activeCount")
    public RespObj<?> getActiveConsultationCount(
            @Parameter(description = "开始时间", required = true, example = "2025-01-01 00:00:00")
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @Parameter(description = "结束时间", required = true, example = "2025-12-31 23:59:59")
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        Long activeCount = consultationService.getActiveConsultationCount(startTime, endTime);
        return new RespObj<Long>().success(activeCount);
    }

    /**
     * 获取已签约的咨询记录数量
     *
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 已签约的咨询记录数量
     */
    @Operation(summary = "获取已签约的咨询记录数量", description = "统计elder_consultation表中指定时间范围内status=2的记录数量")
    @GetMapping("/signedCount")
    public RespObj<?> getSignedConsultationCount(
            @Parameter(description = "开始时间", required = true, example = "2025-01-01 00:00:00")
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @Parameter(description = "结束时间", required = true, example = "2025-12-31 23:59:59")
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        Long signedCount = consultationService.getSignedConsultationCount(startTime, endTime);
        return new RespObj<Long>().success(signedCount);
    }

    /**
     * 获取咨询整体转化率
     *
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 转化率（百分比）
     */
    @Operation(summary = "获取咨询整体转化率", description = "计算指定时间范围内已签约数量占总咨询数量的百分比")
    @GetMapping("/conversionRate")
    public RespObj<?> getConsultationConversionRate(
            @Parameter(description = "开始时间", required = true, example = "2025-01-01 00:00:00")
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @Parameter(description = "结束时间", required = true, example = "2025-12-31 23:59:59")
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        Double conversionRate = consultationService.getConsultationConversionRate(startTime, endTime);
        return new RespObj<Double>().success(conversionRate);
    }

    /**
     * 获取销售人员咨询统计TOP5
     *
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 销售人员咨询统计TOP5
     */
    @Operation(summary = "获取销售人员咨询统计TOP5", description = "统计指定时间范围内销售人员的咨询量、跟进量、签约量和转化率，按转化率排序TOP5")
    @GetMapping("/salesTop5")
    public RespObj<?> getSalesPerformanceTop5(
            @Parameter(description = "开始时间", required = true, example = "2025-01-01 00:00:00")
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @Parameter(description = "结束时间", required = true, example = "2025-12-31 23:59:59")
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        List<Map<String, Object>> salesTop5 = consultationService.getSalesPerformanceTop5(startTime, endTime);
        return new RespObj<List<Map<String, Object>>>().success(salesTop5);
    }

    /**
     * 获取媒介渠道统计信息
     *
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 媒介渠道统计信息
     */
    @Operation(summary = "获取媒介渠道统计信息", description = "统计指定时间范围内各媒介渠道(1-5)的接待次数、签约量和转化率")
    @GetMapping("/channel")
    public RespObj<?> getChannelStatistics(
            @Parameter(description = "开始时间", required = true, example = "2025-01-01 00:00:00")
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @Parameter(description = "结束时间", required = true, example = "2025-12-31 23:59:59")
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        List<Map<String, Object>> channelStats = consultationService.getChannelStatistics(startTime, endTime);
        return new RespObj<List<Map<String, Object>>>().success(channelStats);
    }


    @Operation(summary = "获取咨询列表（分页）", description = "三表联查获取咨询详细信息，支持多条件筛选和分页，使用MyBatis-Plus分页插件实现")
    @PostMapping("/dashboard/list")
    public RespObj<?> getDashboardListPaged(
            @Parameter(description = "咨询人姓名（模糊查询）", example = "张女士")
            @RequestParam(required = false) String consultantName,
            
            @Parameter(description = "咨询状态（0-新咨询，1-跟进中，2-已签约，3-已关闭，4-已失效）", example = "1")
            @RequestParam(required = false) Integer status,
            
            @Parameter(description = "销售人员姓名（模糊查询）", example = "李销售")
            @RequestParam(required = false) String saleName,
            
            @Parameter(description = "咨询开始时间", example = "2025-07-01 00:00:00")
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            
            @Parameter(description = "咨询结束时间", example = "2025-07-31 23:59:59")
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            
            @Parameter(description = "当前页码（从1开始）", example = "1")
            @RequestParam(defaultValue = "1") Long current,
            
            @Parameter(description = "每页数量", example = "10")
            @RequestParam(defaultValue = "10") Long size) {
        
        PageResult<Map<String, Object>> pageResult = consultationService.getDashboardListPaged(
                consultantName, status, saleName, startTime, endTime, current, size);
        return new RespObj<PageResult<Map<String, Object>>>().success(pageResult);
    }
}
